﻿@using System.Linq
@using Mvc.JQuery.DataTables
@using Newtonsoft.Json
@using Newtonsoft.Json.Linq
@model DataTableConfigVm
<table id="@Model.Id" class="display @(Model.TableClass ?? DataTableConfigVm.DefaultTableClass ?? "")" @Html.Raw(Model.FixedLayout ? "style='table-layout:fixed'":"")>
    <thead>
        @if (Model.UseColumnFilterPlugin)
        {
            <tr>
                @foreach (var column in Model.Columns)
                {
                    <th >@column.DisplayName</th>
                }
            </tr>
        }
        @if (!Model.HideHeaders)
        {
            <tr>
                @foreach (var column in Model.Columns)
                {
                    <th class="@column.CssClassHeader">@column.DisplayName</th>
                }
            </tr>
        }
    </thead>
    <tbody>
        <tr>
            <td colspan="@Model.Columns.Count()" class="dataTables_empty">Loading data from server
            </td>
        </tr>
    </tbody>
</table>

<script type="text/javascript">
    (function setDataTable() {
        if(!window.jQuery || !$.fn.DataTable) {
            setTimeout(setDataTable, 100);
            return;
        }
        var $table = $('#@Model.Id');

        @{
            var options = new JObject();
            options["aaSorting"] = new JRaw(Model.ColumnSortingString);
            options["bProcessing"] = true;
            options["bStateSave"] = Model.StateSave;
            options["bServerSide"] = true;
            options["bFilter"] = Model.Filter;
            options["sDom"] = Model.Dom;
            if (Model.LengthMenu != null)
            {
                options["lengthMenu"] = new JRaw(Model.LengthMenu);
            }
            if (Model.PageLength.HasValue)
            {
                options["pageLength"] = Model.PageLength;
            }
            options["bAutoWidth"] = Model.AutoWidth;
            options["sAjaxSource"] = Model.AjaxUrl;
            if (Model.TableTools)
            {
                options["oTableTools"] = new JRaw("{ 'sSwfPath': '//cdn.datatables.net/tabletools/2.2.1/swf/copy_csv_xls_pdf.swf' }");
            }
            options["fnServerData"] = new JRaw(
                "function(sSource, aoData, fnCallback) { " + 
                "    var ajaxOptions = { 'dataType': 'json', 'type': 'POST', 'url': sSource, 'data': aoData, 'success': fnCallback }; " +
                (Model.AjaxErrorHandler == null ? "" : ("ajaxOptions['error'] = " + Model.AjaxErrorHandler) + "; ") +
                "    $.ajax(ajaxOptions);" +
                "}");
            options["aoColumnDefs"] = new JRaw(Model.ColumnDefsString);
            options["aoSearchCols"] = Model.SearchCols;
            if (Model.JsOptions.Any())
            {
                foreach (var jsOption in Model.JsOptions)
                {
                    options[jsOption.Key] = new JRaw(jsOption.Value);
                }
            }
            if (!string.IsNullOrWhiteSpace(Model.Language))
            {
                options["oLanguage"] = new JRaw(Model.Language);
            }
            if (!string.IsNullOrWhiteSpace(Model.DrawCallback))
            {
                options["fnDrawCallback"] = new JRaw(Model.DrawCallback);
            }
            
        }

        var dt = $table.dataTable(@Html.Raw(options.ToString(Formatting.Indented)));
        @if (Model.UseColumnFilterPlugin)
        {
            @Html.Raw("dt.columnFilter(" + Model.ColumnFilterVm + ");")
        }
        
        @if (Model.GlobalJsVariableName != null)
        {
            @Html.Raw("window['" + Model.GlobalJsVariableName + "'] = dt;")
        }
    })();
</script>
